<template>
  <view class="send">
    <view class="changeView">
      <view class="changeBtn" @click="handleScan">
        扫码核销
      </view>
    </view>
    <view class="cardItem">
      <view>
        <view class="title">请输入卡号</view>
        <view class="uni-input-wrapper">
          <input class="uni-input" v-model.trim="couponId" placeholder="请输入卡号" />
          <uni-icons type="scan" size="26" class="scanIcon" @click="scan"></uni-icons>
        </view>
      </view>
      <view>
        <view class="title">请输入卡密</view>
        <view class="uni-input-wrapper">
          <input class="uni-input" v-model.trim="password" password placeholder="请输入卡密" />
        </view>
      </view>
      <view class="cardButton">
        <view class="btn" @click="handleChange">提交</view>
      </view>
    </view>
  </view>
</template>

<script>
import { verification } from '@/api/card.js';
export default {
  data() {
    return {
      couponId: '',
      password: ''
    };
  },
  methods: {
    scan() {
      // 调用扫码功能
      uni.scanCode({
        success: (res) => {
          console.log('扫码成功:', res);
          this.couponId = res.result; // 获取扫码结果
        },
        fail: (err) => {
          console.log('扫码失败:', err);
          uni.showToast({
            title: '扫码失败',
            icon: 'none'
          });
        }
      });
    },
    handleChange() {
      if (this.couponId && this.password) {
        verification({ merchantId: this.userInfo.id, couponId: this.couponId, password: this.password }).then((res) => {
          console.log('res:', res);
          if (res.code == 200) {
            this.couponId = '';
            this.password = '';
            uni.showToast({
              title: '核销成功',
              icon: 'success'
            });
          }
        });
      } else {
        uni.showToast({
          title: '请输入卡号和卡密',
          icon: 'none'
        });
      }
      uni.navigateTo({
        url: '/pages/card/change/change'
      });
    },
    handleScan() {
      uni.switchTab({
        url: '/pages/index/scan/scan'
      });
    }
  },
  computed: {
    userInfo: {
      get() {
        return this.$store.getters.userInfo;
      },
      set() {
      }
    }
  },
  watch: {

  },
  // 页面周期函数--监听页面加载
  onLoad(options) {
    if (options.result) {
      this.couponId = options.result;
    }
  },
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示
  onShow() {},
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {},
  // 页面处理函数--监听用户下拉动作
  onPullDownRefresh() {},
  // 页面上拉触底事件的处理函数
  onReachBottom() {},
  // 用户点击右上角分享
  onShareAppMessage() {},
  // 页面处理函数--监听页面滚动(not-nvue)
  onPageScroll(e) {}
};
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>
